﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Twodi.Structs
{
    public class AnimationFrame
    {
        protected TimeSpan time;

        protected Rectangle source;

        public AnimationFrame()
        {

        }

        public AnimationFrame(TimeSpan time)
        {
            this.Time = time;
        }

        public static AnimationFrame CreateFrame(TimeSpan time, Rectangle source)
        {
            return new AnimationFrame(time) { Source = source };
        }

        public static AnimationFrame CreateFrame(TimeSpan time, int x, int y, int widht, int height)
        {
            return new AnimationFrame(time) { Source = new Rectangle(x, y, widht, height) };
        }

        public TimeSpan Time
        {
            get { return this.time; }
            set
            {
                this.time = value;
                onTimeChanged(EventArgs.Empty);
            }
        }

        public Rectangle Source
        {
            get { return this.source; }
            set
            {
                this.source = value;
                onSourceChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> TimeChanged;

        public event EventHandler<EventArgs> SourceChanged;

        protected virtual void onTimeChanged(EventArgs e)
        {
            if (TimeChanged != null)
            {
                TimeChanged(this, e);
            }
        }

        protected virtual void onSourceChanged(EventArgs e)
        {
            if (SourceChanged != null)
            {
                SourceChanged(this, e);
            }
        }

        public AnimationFrame Copy(TimeSpan time = new TimeSpan(), Rectangle source = new Rectangle())
        {
            if (time == TimeSpan.Zero)
            {
                time = this.Time;
            }
            if (source == Rectangle.Empty)
            {
                source = this.Source;
            }

            return new AnimationFrame(time) { Source = source };
        }
    }
}
